package com.yupi.springbootinit.controller;

import com.qcloud.cos.transfer.MultipleFileUpload;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.FileConstant;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.file.UploadFileRequest;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.enums.FileUploadBizEnum;
import com.yupi.springbootinit.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api
@RestController
@RequestMapping("/fileUpload")
@CrossOrigin
public class FileUploadController {

    @Resource
    private FileService fileService;

    @Resource
    private OrdersService ordersService;

    @Resource
    private UserService userService;

    @Resource
    private TeachersService teachersService;

    @Resource
    private TemporaryordersService temporaryordersService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public BaseResponse<String> uploadFile(MultipartFile file, UploadFileRequest uploadFileRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        String biz = uploadFileRequest.getBiz();
        FileUploadBizEnum enumValue = FileUploadBizEnum.getEnumByValue(biz);
        if (enumValue == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"没有这种类型的文件");
        }
        String url = fileService.uploadFile(file, enumValue, userId);
        return ResultUtils.success(url);
    }

}
